package com.songyuquan.webcs.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.songyuquan.webcs.mapper.IntegralsMapper;
import com.songyuquan.webcs.pojo.*;
import com.songyuquan.webcs.service.*;
import com.songyuquan.webcs.vo.OrdersListVO;
import com.songyuquan.webcs.vo.ProductListVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * 前台主页控制器
 */
@Controller
public class MainController {

    @Autowired
    private CartService cartService;

    @Autowired
    private OrdersService ordersService;

    @Autowired
    private OrderDetailService orderDetailService;

    @Autowired
    private ProductService productService;

    @Autowired
    private UserAddressService userAddressService;

    @Autowired
    private IntegralsMapper integralsMapper;

    //显示前台页面信息
    @GetMapping("/")
    public String mainPage(HttpSession session) {
        return "forward:/productCategory/list";
    }

    //获取路径
    @GetMapping("/{url}")
    public String main2(@PathVariable("url") String url) {
        return url;
    }

    //跳转到登录页
    @GetMapping("/login")
    public String getLoginPage() {
        return "login";
    }

    //跳转到注册页
    @GetMapping("/register")
    public String getRegisterPage(HttpSession session, Model model) {
        String error = (String) session.getAttribute("error");
        if (error != null) model.addAttribute("error", error);
        return "register";
    }

    //用户个人中心
    @GetMapping("/userInfo")
    public ModelAndView userinfoPage(HttpSession session) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("userInfo");
        User user = (User) session.getAttribute("user");
        modelAndView.addObject("userinfo", user);
        modelAndView.addObject("cartList", cartService.findAllCartVOByUserId(user.getId()));
        return modelAndView;
    }

    //我的订单
    @GetMapping("/orderList")
    public ModelAndView orderListPage(HttpSession session) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("orderList");
        User user = (User) session.getAttribute("user");
        //订单列表
        QueryWrapper<Orders> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", user.getId());
        List<Orders> list = ordersService.list(queryWrapper);
        //封装订单
        List<OrdersListVO> ordersListVOS = new ArrayList<>();
        OrdersListVO ordersListVO = null;
        for (Orders orders : list) {
            //封装每一个订单
            ordersListVO = new OrdersListVO();
            ordersListVO.setUserName(user.getUserName());
            ordersListVO.setUserAddress(orders.getUserAddress());
            ordersListVO.setSerialnumber(orders.getSerialnumber());
            ordersListVO.setCost(orders.getCost());
            ordersListVO.setIntegral(orders.getIntegral());
            ordersListVO.setGoods(orders.getGoods());
            //获取订单详情
            QueryWrapper<OrderDetail> queryWrapper2 = new QueryWrapper<>();
            queryWrapper2.eq("order_id", orders.getId());
            List<OrderDetail> orderDetailList = orderDetailService.list(queryWrapper2);
            List<ProductListVO> productListVOS = new ArrayList<>();
            ProductListVO productListVO = null;
            //封装每一个订单详情
            for (OrderDetail orderDetail : orderDetailList) {
                productListVO = new ProductListVO();
                Product product = productService.getById(orderDetail.getProductId());
                productListVO.setName(product.getName());
                productListVO.setFileName(product.getFileName());
                productListVO.setPrice(product.getPrice());
                productListVO.setCost(orderDetail.getCost());
                productListVO.setQuantity(orderDetail.getQuantity());
                productListVO.setIntegral(orderDetail.getIntegral());
                productListVOS.add(productListVO);
            }
            ordersListVO.setProductList(productListVOS);
            ordersListVOS.add(ordersListVO);
        }
        modelAndView.addObject("orderList", ordersListVOS);
        modelAndView.addObject("cartList", cartService.findAllCartVOByUserId(user.getId()));
        return modelAndView;
    }

    //我的积分
    @GetMapping("/integralList")
    public ModelAndView integralListPage(HttpSession session) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("integralList");
        User user = (User) session.getAttribute("user");
        //订单列表
        QueryWrapper<Orders> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", user.getId());
        List<Orders> list = ordersService.list(queryWrapper);
        //封装订单
        List<OrdersListVO> ordersListVOS = new ArrayList<>();
        OrdersListVO ordersListVO;
        Integer integerTotal = 0;
//        Integrals integrals = integralsMapper.selectOne(queryWrapper);
//        if (integrals == null) {
//            integerTotal = 0;
//        }
        for (Orders orders : list) {
            //封装每一个订单
            ordersListVO = new OrdersListVO();
            ordersListVO.setUserName(user.getUserName());
            ordersListVO.setUserAddress(orders.getUserAddress());
            ordersListVO.setSerialnumber(orders.getSerialnumber());
            ordersListVO.setCost(orders.getCost());
            ordersListVO.setIntegral(orders.getIntegral());
            ordersListVO.setGoods(orders.getGoods());
            ordersListVO.setProductList(null);
            ordersListVOS.add(ordersListVO);
            integerTotal += orders.getIntegral();
        }
        modelAndView.addObject("orderList", ordersListVOS);
        modelAndView.addObject("integerTotal", integerTotal);
        modelAndView.addObject("cartList", cartService.findAllCartVOByUserId(user.getId()));
        return modelAndView;
    }

    //地址管理
    @GetMapping("/userAddressList")
    public ModelAndView userAddressListPage(HttpSession session) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("userAddressList");
        User user = (User) session.getAttribute("user");
        QueryWrapper<UserAddress> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", user.getId());
        List<UserAddress> userAddressList = userAddressService.list(queryWrapper);
        modelAndView.addObject("userAddressList", userAddressList);
        modelAndView.addObject("cartList", cartService.findAllCartVOByUserId(user.getId()));
        return modelAndView;
    }
}
